package com.im.services.utils;

import com.alibaba.fastjson.JSONPath;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
public class ChatGptUtils {

    public String request(Map<String, String> param) {

        String apiKey = param.get("apiKey");
        String apiUrl = param.get("apiUrl");
        String maxToken = param.get("maxToken");
        String model = param.get("model");
        String content = param.get("content");

        // 构建请求的消息列表
        String requestBody = "{\n" +
                "    \"messages\": [\n" +
                "        {\"role\": \"system\", \"content\": \"" + content + "\"}\n" +
//                "        {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"}\n" +
                "    ],\n" +
                "    \"max_tokens\": " + maxToken + ",\n" +
                "    \"model\": \"" + model + "\"\n" +
                "}";

        log.debug("RequsetBody:{}", requestBody);

        // 创建HttpClient
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建HttpPost
            HttpPost httpPost = new HttpPost(apiUrl);

            // 设置请求头
            httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + apiKey);
            httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType());

            // 设置请求体
            httpPost.setEntity(new StringEntity(requestBody, ContentType.APPLICATION_JSON));

            // 发送请求并获取响应
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            // 处理API响应
            if (entity != null) {
                String responseBody = EntityUtils.toString(entity);

                if (response.getStatusLine().getStatusCode() == 200) {
                    // 打印生成的回复
                    System.out.println("API Request success: " + responseBody);
//                    Map result = JSON.parseObject(responseBody, Map.class);
//                    result.get("")
                    String read = JSONPath.read(responseBody, "$.choices.message.content",String.class);
//                    System.out.println(read);
                    return read;
                } else {
                    // 打印错误信息
                    System.out.println("API Request Failed: " + response.getStatusLine().getStatusCode() + " " + responseBody);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
